package org.example.im.client.pipeline;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import lombok.RequiredArgsConstructor;
import org.example.im.client.handler.*;
import org.example.im.common.codec.PacketCodecHandler;
import org.example.im.common.codec.Spliter;
import org.example.im.common.idle.IdleHandler;
import org.springframework.stereotype.Component;

/**
 * @author dingrui
 * @since 2022/2/10
 * @description
 */
@Component
@RequiredArgsConstructor
public class ClientInitializer extends ChannelInitializer<SocketChannel> {

    private final IdleHandler idleHandler;
    private final Spliter spliter;
    private final PacketCodecHandler packetCodecHandler;
    private final LoginHandler loginHandler;
    private final Send2UserHandler send2UserHandler;
    private final CreateGroupHandler createGroupHandler;
    private final JoinGroupHandler joinGroupHandler;
    private final LeaveGroupHandler leaveGroupHandler;
    private final ListGroupMemberResponseHandler listGroupMemberResponseHandler;
    private final Send2GroupHandler send2GroupHandler;
    private final LogoutHandler logoutHandler;
    private final HeartBeatHandler heartBeatHandler;

    @Override
    public void initChannel(SocketChannel socketChannel) throws Exception {
        // 客户端handler不存在share问题
        socketChannel.pipeline()
                .addLast(this.idleHandler) // 空闲
                .addLast(this.spliter) // 拆包
                .addLast(this.packetCodecHandler) // 编解码
                .addLast(this.loginHandler) // 登录
                .addLast(this.send2UserHandler) // 发送消息
                .addLast(this.createGroupHandler) // 创建群
                .addLast(this.joinGroupHandler) // 加群
                .addLast(this.leaveGroupHandler) // 退群
                .addLast(this.listGroupMemberResponseHandler) // 群成员
                .addLast(this.send2GroupHandler) // 群消息
                .addLast(this.logoutHandler) // 登出
                .addLast(this.heartBeatHandler); // 心跳
    }
}
